import numpy as np
import pandas as pd
from lazypredict.Supervised import LazyClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from util import createXY  # 确保这个函数是从正确的文件导入的

# 创建数据集的特征和标签
X, y = createXY(train_folder="./data/train", dest_folder=".", method='flat')

# 将 X 和 y 转换为 pandas DataFrame 和 Series
X = pd.DataFrame(X)
y = pd.Series(y)

# 检查数据是否有缺失值
if X.isnull().values.any() or y.isnull().values.any():
    print("数据中存在缺失值，请进行处理。")
    exit()

# 如果 y 中包含非数值类型，使用 LabelEncoder 进行转换
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

# 使用 lazypredict 进行模型比较
clf = LazyClassifier(verbose=0, ignore_warnings=True, custom_metric=None)
try:
    models, predictions = clf.fit(X_train, y_train, X_test, y_test)
    if models.empty:
        print("模型性能比较结果为空，请检查数据源或模型配置。")
    else:
        print(models)
except Exception as e:
    print(f"模型比较过程中发生错误：{e}")